package com.itec.learn.log;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author chenyiqun
 */
public class LogServlet extends HttpServlet {

    private static final Logger logger = LoggerFactory.getLogger(LogServlet.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        String userName = req.getParameter("userName");
        String passwd = req.getParameter("passWord");
        logger.info("Log Servlet has been called. userName = {}, passWd = {}.",userName,passwd);
        try {
            if(userName.equals("admin")){
                logger.info("{} has login successfully.",userName);
                PrintWriter pw = resp.getWriter();
                pw.write("Login Success");
                pw.close();
            }
        } catch (Exception e){
            logger.error("Login error.",e);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }
}
